package com.bite.common.core.domain;

import com.bite.common.core.enums.ResultCode;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class R<T> {
    private  int code;    // 定义一些统一的code ,前后端商量好的，比如返回0失败，1成功，2，3什么什么错了，比如账号密码啥的，枚举

    private  String msg;  //?    通常作为code的辅助说明，一个code对应一个msg

    private T data;       //请求某个接口返回的数据list SysUser 泛型

    public static <T> R<T>ok(){
        return assembleResult(null, ResultCode.SUCCESS);
    }
    public static <T> R<T>ok(T data){
        return assembleResult(data, ResultCode.SUCCESS);
    }
    public static <T> R<T>fail(){
        return assembleResult(null, ResultCode.FAILED);
    }

    /**
     *
     * @param resultCode 制定错误码
     * @param <T>
     * @return
     */
    public static <T> R<T>fail(ResultCode resultCode){
        return assembleResult(null,resultCode);
    }
    private static <T>R<T> assembleResult(T data,ResultCode resultCode){
        R<T> r=new R<>();
        r.setCode(resultCode.getCode());
        r.setData(data);
        r.setMsg(resultCode.getMsg());
        return r;
    }

   //自定义Code和 Massage
    public static<T> R<T> fail(int code, String msg) {
        return assembleResult(code,msg,null);
    }
    private static <T>R<T> assembleResult(int code, String msg,T data){
        R<T> r=new R<>();
        r.setCode(code);
        r.setData(data);
        r.setMsg(msg);
        return r;
    }
}
